// pages/selectGoods/selectGoods.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goods_attrSelect: [],
    curvIndex: 0,
    select_num: 1,
    cover_img:"",
    memory:"",
    key:"",
    goods_id:""
  },
  selectVersion(e) {
    const version = e.detail.value;
    console.log(version);
    const memory = version.split(",")[0];
    const key = version.split(",")[1];
    const goods_id=version.split(",")[2];

    this.setData({
      memory,
      key,
      goods_id
    });
  },
  versionHasSelected(e) {
    const curvIndex = e.currentTarget.dataset.index ? e.currentTarget.dataset.index : 0;
    console.log(curvIndex);
    this.setData({
      curvIndex
    });
  },
  minusCount(e) {
    let select_num = this.data.select_num;
    select_num--;
    if (select_num < 1) {
      return;
    }
    this.setData({
      select_num
    });
    wx.setStorageSync('select_num', select_num);
  },
  addCount(e) {
    let select_num = this.data.select_num;
    select_num++;
    if (select_num > 5) {
      return;
    }
    this.setData({
      select_num
    });
    wx.setStorageSync('select_num', select_num);
  },
  submit(e) {
    var that=this;
    var item_id=this.data.goods_id;
    var item_sku_id=this.data.key;
    var goods_num=this.data.select_num;
    app.getUserToken(function (err, token) {
    wx.request({
      url: app.globalData.url +'/index.php?s=api/cart/add',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        method:'get',
        data: {item_id:item_id,item_sku_id:item_sku_id,goods_num:goods_num,app_id:10001,token:app.globalData.token},
        success: (res) => {
            if(res.data.code==1){
              wx.showModal({
                title: '加入购物车成功',
                content: '是否进入购物车',
                success(res) {
                  if (res.confirm) {
                    wx.switchTab({
                      url: '../cart/cart'
                    })
                  }
                }
              })
            }
        }
      })
    })
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const id=options.id;
    this.selectAttr(id);
  },

  selectAttr:function(options) {
    var that = this;
    wx.showLoading({
      title: '加载中...'
    })
    wx.request({
      url: app.globalData.url +'/index.php?s=api/item/detail',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        method:'get',
        data: {item_id:options,app_id:10001},
        success: (res) => {
          if (res.data.code == 1){
            wx.hideLoading()
            that.setData({
              goods_attrSelect: res.data.data.detail,
              memory:res.data.data.detail.sku[0].item,
              key:res.data.data.detail.sku[0].key,
              goods_id:res.data.data.detail.sku[0].goods_id
            })
          }else{
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 1500
            })
          }
         
        }
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const version = wx.getStorageSync('version')
    // this.getGoods(version)
    console.log(version)
    wx.setStorageSync('select_num', this.data.select_num);
    // wx.setStorageSync('color', this.data.color);
    // wx.setStorageSync('memory', this.data.memory);
    // wx.setStorageSync('price', this.data.price);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})